#include "createwindowbyinfo.h"

QProcess* CreateWindowByInfo::pro = new QProcess();
CreateWindowByInfo::CreateWindowByInfo()
{

}

/**
 * @brief getWidget     根据窗口类和窗口名称找Widget
 * @param lpClassName
 * @param lpWindowName
 * @return
 */
QWidget *CreateWindowByInfo::getWidgetByWId(LPCWSTR lpClassName, LPCWSTR lpWindowName, QWidget* parent)
{
   return QWidget::createWindowContainer(QWindow::
                                         fromWinId((WId)FindWindow(lpClassName, lpWindowName)),parent);
}

/**
 * @brief getWidget     根据窗口类和窗口名称找Widget
 * @param lpClassName
 * @param lpWindowName
 * @return
 */
QWidget *CreateWindowByInfo::getWidgetByWId(const QString &fileName, LPCWSTR lpClassName, LPCWSTR lpWindowName, QWidget *parent)
{
    if (!QFile::exists(fileName)){
        return (QWidget*)nullptr;
    }
    CreateWindowByInfo::pro->start(fileName,QStringList());
    CreateWindowByInfo::pro->waitForFinished(100);
    return CreateWindowByInfo::getWidgetByWId(lpClassName, lpWindowName, parent);
}



HWND CreateWindowByInfo::NIScopeWId=0;
BOOL CALLBACK EnumChildProc(HWND hwnd,LPARAM lParam)
{
    //枚举子窗口的回调函数
    TCHAR lpWinTitle[256],lpClassName[256];
    ::GetWindowTextW(hwnd,(LPWSTR)lpWinTitle,256-1); //获得窗口caption
    ::GetClassNameW(hwnd,(LPWSTR)lpClassName,256-1); //获得窗口类名
    if (wcsicmp((LPWSTR)lpWinTitle,L"")!=NULL && wcsicmp((LPWSTR)lpClassName,L"")!=NULL) //过滤掉没有名子和类名的窗口
    {
        EnumChildWindows(hwnd,EnumChildProc,0); //递归枚举子窗口
    }

    return TRUE ;
}
BOOL CALLBACK EnumWindowsProc(HWND hwnd,LPARAM lparam)
{
    //枚举窗口的回调函数
    TCHAR lpWinTitle[256],lpClassName[256];
    ::GetWindowTextW(hwnd,lpWinTitle,256-1); //获得窗口caption
    ::GetClassNameW(hwnd,lpClassName,256-1); //获得窗口类名
    if (wcsicmp(lpWinTitle,L"")!=NULL && wcsicmp(lpClassName,L"")!=NULL) //过滤掉没有名子和类名的窗口
    {
        QString windowName = QString::fromWCharArray(lpWinTitle);
        const char* name = (const char*)lparam;
        QString strName = QString(name);
        if (windowName.contains(strName))
        {
            CreateWindowByInfo::NIScopeWId = hwnd;
            return FALSE;
        }
        EnumChildWindows(hwnd,EnumChildProc,0); //继续枚举子窗口，传递给回调函数该子窗口的句柄
    }
    return TRUE ;
}

/**
 * @brief getWidgetByWindowName         根据窗口名找窗口
 * @param WindowName
 * @param parent
 * @return
 */
QWidget *CreateWindowByInfo::getWidgetByWindowName(const QString &fileName, const char* WindowName, QWidget *parent)
{
    if (!QFile::exists(fileName)){
        return (QWidget*)nullptr;
    }
    pro->start(fileName, QStringList());
    pro->waitForFinished(100);
    while (!CreateWindowByInfo::NIScopeWId){
        EnumWindows(EnumWindowsProc,(LPARAM)WindowName); //枚举桌面的窗口，每找到一个窗口，调用一次回调函数，传递给回调函数该窗口的句柄
    }
    QWidget* widget = QWidget::createWindowContainer(QWindow::fromWinId((WId)CreateWindowByInfo::NIScopeWId),parent);
    CreateWindowByInfo::NIScopeWId = 0;
    return widget;
}

/**
 * @brief close     关闭进程
 */
void CreateWindowByInfo::close()
{
   if (pro->state() == QProcess::Running){
       pro->close();
   }
}





